package com.sdy.supermarket.biz.service.impl;

import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.supermarket.biz.mapper.GsHomeManageMapper;
import com.sdy.supermarket.biz.model.GsBannerManage;
import com.sdy.supermarket.biz.mapper.GsBannerManageMapper;
import com.sdy.supermarket.biz.model.GsHomeManage;
import com.sdy.supermarket.biz.service.GsBannerManageService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zzq
 * @since 2019-07-30
 */
@Slf4j
@Service
public class GsBannerManageServiceImpl extends BaseServiceImpl<GsBannerManage> implements GsBannerManageService {
    @Resource
    private GsBannerManageMapper gsBannerManageMapper;
    @Resource
    private GsHomeManageMapper gsHomeManageMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Response saveBannerManage(GsBannerManage gsBannerManage) throws BizException {
        Date now = new Date();
        gsBannerManage.setLastUpdateTime(now);
        gsBannerManage.setCreateTime(now);
        boolean save = saveOrUpdate(gsBannerManage);
        // 更新首页管理的时间
        int homeUpdate = gsHomeManageMapper.updateById(new GsHomeManage().setId(1).setLastUpdateTime(now));
        Assert.isTrue(!save || homeUpdate == 0, "新增失败");
        return Response.success("新增成功");
    }

    @Override
    public List<GsBannerManage> listValidBanner() {
        return lambdaQuery().orderByDesc(GsBannerManage::getId).list();
    }

}
